package com.magenta.maxoptra.geocoding.yahoo;

import com.magenta.maxoptra.geocoding.yahoo.mapping.XmlYahooMappingFunctor;
import com.magenta.maxoptra.gis.*;
import com.magenta.maxoptra.gis.commons.StringUtils;
import com.magenta.mrs.basegeocoding.BaseGeocoding;
import com.magenta.mrs.geocoding.connector.GeoAddressStandardizer;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/*
* Project: Maxoptra
* Author: Kirill Sychin
* Date: 01.11.2008 16:57:17
*
* Copyright (c) 1999-2008 Magenta Corporation Ltd. All Rights Reserved.
* Magenta Technology proprietary and confidential.
* Use is subject to license terms.
*/

public class GeocodingYahooImpl extends BaseGeocoding<String> implements IGeocoding {

    /*
        public static void main(String[] args) {
            IGeocoding iGeocoding = new GeocodingYahooImpl();
            List<GeoAddress> addressList = iGeocoding.findAddressByKeyword("RU", "Moscow", 5);
            System.out.println(addressList);
        }
    */

    public String getProviderName() {
        return MapProviderType.yahoo.name();
    }

    public boolean accuracyIsAccurateEnough(String accuracyThreshold, String accuracy) {
        return accuracy.equals(accuracyThreshold);
    }

    class YahooParameters {
        private String serverUrl = null;
        private String mapKey = null;

        public YahooParameters(String serverUrl, String mapKey) {
            this.mapKey = mapKey;
            this.serverUrl = serverUrl;
        }

        public String getServerUrl() {
            return serverUrl;
        }

        public String getMapKey() {
            return mapKey;
        }
    }

    private YahooParameters getYahooParameters() throws GeoException {
        GisParameters gisParams = getProviderParams();
        String serverUrl;
        String mapKey;
        String providerName = getProviderName();
        if (gisParams != null) {
            serverUrl = gisParams.get(GisParameter.base_url.withProvider(providerName));
            if (StringUtils.isEmpty(serverUrl)) {
                throw new GeoException("Parameter " + GisParameter.base_url + " is not set for " + providerName);
            }
            mapKey = gisParams.get(GisParameter.map_key.withProvider(providerName));
            if (StringUtils.isEmpty(mapKey)) {
                throw new GeoException("Parameter " + GisParameter.map_key + " is not set for " + providerName);
            }
        } else {
            throw new GeoException("Gis parameters is not set for " + providerName);
        }
        return new YahooParameters(serverUrl, mapKey);
    }


    protected List<GeoAddress> doFindAddressByKeyword(GeoProviderSettings settings, String keyword, int i, List<String> accuracies)
            throws GeoException {
        List<GeoAddress> list = new ArrayList<GeoAddress>();
        YahooParameters gisParams = getYahooParameters();
        GeoAddressStandardizer addressStandardizer;
//        if (proxy != null && proxy.isEnable()) {
//            Credentials defaultcreds = new UsernamePasswordCredentials(proxy.getUser(), proxy.getPassword());
//            addressStandardizer = new GeoAddressStandardizer(defaultcreds, proxy.getHost(), proxy.getPort());
//        } else {
        addressStandardizer = new GeoAddressStandardizer();
//        }

        String urlString = MessageFormat.format(gisParams.getServerUrl(), gisParams.getMapKey(), keyword);

        XmlYahooMappingFunctor mappingFunctor = XmlYahooMappingFunctor.getInstance();
        mappingFunctor.setAccuracies(accuracies);

        list = addressStandardizer.standardize(urlString, mappingFunctor);
        if (list.size() > i) {
            list = list.subList(0, i - 1);
        }

        return list;
    }

    @Override
    public List<GeoAddress> doForwardGeocoding(GeoProviderSettings settings, GeoAddress address, int resultAddressLimit)
            throws GeoException {
        throw new UnsupportedOperationException("");
    }

    public GeoAddress findCountry(String country, String keyword) throws GeoException {
        throw new UnsupportedOperationException("IGeocoding.findCountry is not implemented for Yahoo");
    }

}
